package com.zg.shop.order;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.zg.shop.common.Constant;
import com.zg.shop.product.PageBean;

@Transactional
public class OrderService {

	private OrderDao orderDao;
	
	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public Integer saveOrder(Order order) {
		return orderDao.saveOrder(order);
	}

	public Order findByOid(Integer oid) {
		
		return orderDao.findByOid(oid);
	}

	public void update(Order currOrder) {
		// TODO Auto-generated method stub
		orderDao.update(currOrder);
	}

	public List<Order> findByUid(Integer uid) {
		return orderDao.findByUid(uid);
	}

	public PageBean<Order> findByPage(Integer page) {
		int totalCount = 0;
		int totalPage = 0;
		
		PageBean<Order> pageBean = new PageBean<Order>();
		pageBean.setLimit(Constant.CLIMIT);
		pageBean.setPage(page);
		totalCount = orderDao.findCount();
		pageBean.setTotalCount(totalCount);
		if(totalPage % Constant.CLIMIT == 0){
			totalPage = totalCount/Constant.CLIMIT;
		}else{
			totalPage = totalCount/Constant.CLIMIT + 1;
		}
		pageBean.setTotalPage(totalPage);
		Integer begin = (page - 1)*Constant.CLIMIT;
		List<Order> orders = orderDao.findByPage(begin, Constant.CLIMIT);
		pageBean.setList(orders);
		return pageBean;
	}

	public PageBean<Order> findByPage(Integer page, Integer state) {
		int totalCount = 0;
		int totalPage = 0;
		
		PageBean<Order> pageBean = new PageBean<Order>();
		pageBean.setLimit(Constant.CLIMIT);
		pageBean.setPage(page);
		totalCount = orderDao.findCount(state);
		pageBean.setTotalCount(totalCount);
		if(totalPage % Constant.CLIMIT == 0){
			totalPage = totalCount/Constant.CLIMIT;
		}else{
			totalPage = totalCount/Constant.CLIMIT + 1;
		}
		pageBean.setTotalPage(totalPage);
		Integer begin = (page - 1)*Constant.CLIMIT;
		List<Order> orders = orderDao.findByPage(begin, Constant.CLIMIT, state);
		pageBean.setList(orders);
		return pageBean;

	}

}
